@include('admin.head')
<div class="container">
	<div class="row">
		@include('admin.side')
		<div class="col-md-10" id="content-wrapper">
			<div class="row">
				<div class="col-xs-12">
					<div class=" clearfix">
						<div class="clearfix">
							<h2 class="pull-left">项目编辑</h2>
						</div>
						<div class="panel panel-default">
									<div class="panel-body">
					<div class="form-horizontal">
					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">项目名称:</label>
					      <div class="col-xs-4">
					        <input class="form-control input_size" name="name" placeholder="" type="text">
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">投资规模(万元):</label>
					      <div class="col-xs-4">
					        <input class="form-control input_size" name="investment_scale" placeholder="" type="text">
					      </div>
					    </div>


					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">投资模式:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size" name="investment_type_id">
					          <option value="">请选择</option>
					        </select>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">节能效率:</label>
					      <div class="col-xs-4">
					        <input class="form-control input_size" name="efficiency" autocomplete="of" placeholder="" type="text">
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">项目启动时间:</label>
					      <div class="col-xs-4">
					        <input class="form-control input_size date-time-picker" readonly name="start_time" autocomplete="of" placeholder="" type="text">
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">项目结束时间:</label>
					      <div class="col-xs-4">
					        <input class="form-control input_size date-time-picker" readonly name="end_time" autocomplete="of" placeholder="" type="text">
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">省:</label>
					      <div class="col-xs-4">
					      	<select class="form-control fl" name="province" style="float: left;" id="province">
					      	  <option value="-1">请选择</option>
					      	</select>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">市:</label>
					      <div class="col-xs-4">
					      	<select class="form-control fl" name="city" style="float: left; " id="city">
					      	  <option value="-1">请选择</option>
					      	</select>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">区:</label>
					      <div class="col-xs-4">
					      	<select class="form-control fl" name="area" style="float: left; " id="area">
					      	  <option value="-1">请选择</option>
					      	</select>
					      </div>
					    </div>

						<div class="form-group">
						  <label class="col-xs-4 control-label no_padding_right">详细地址:</label>
						  <div class="col-xs-4">
						    <input class="form-control input_size" name="address" autocomplete="of" placeholder="" type="text">
						  </div>
						</div>

						<div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">是否资产化:</label>
					      <div class="col-xs-4">
					        <div class="radio row">
					        	<div class="col-xs-2">
						          	<label>
						            <input name="is_request_assets" id="" value="0" checked="checked" type="radio">
						            否
						          	</label>
						        </div>
						        <div class="col-xs-6">
							        <label>
							            <input name="is_request_assets" id="" value="1" type="radio" >
						            	是
					          		</label>
					          	</div>
					        </div>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">是否申请奖励:</label>
					      <div class="col-xs-4">
					        <div class="radio row">
					        	<div class="col-xs-2">
						          	<label>
						            <input name="is_request_award" id="" value="0" checked="checked" type="radio">
						            否
						          	</label>
						        </div>
						        <div class="col-xs-6">
							        <label>
							            <input name="is_request_award" id="" value="1" type="radio" >
						            	是
					          		</label>
					          	</div>
					        </div>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">是否申请补贴:</label>
					      <div class="col-xs-4">
					        <div class="radio row">
					        	<div class="col-xs-2">
						          	<label>
						            <input name="is_request_subsidy" id="" value="0" checked="checked" type="radio">
						            否
						          	</label>
						        </div>
						        <div class="col-xs-6">
							        <label>
							            <input name="is_request_subsidy" id="" value="1" type="radio" >
						            	是
					          		</label>
					          	</div>
					        </div>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">是否申请退税:</label>
					      <div class="col-xs-4">
					        <div class="radio row">
					        	<div class="col-xs-2">
						          	<label>
						            <input name="is_request_rebate" id="" value="0" checked="checked" type="radio">
						            否
						          	</label>
						        </div>
						        <div class="col-xs-6">
							        <label>
							            <input name="is_request_rebate" id="" value="1" type="radio" >
						            	是
					          		</label>
					          	</div>
					        </div>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">项目图片:</label>
					      <div class="col-xs-4">
					        <a href="javascript:;" class="file btn ">
					            <input type="file" name="imgFile" id="projectImg">
					            <input type="hidden" class="hiddenName" name="image" value="0">
					        </a><span class="fileName"></span>
					      </div>
					    </div>

					    <div class="form-group">
					      <label class="col-xs-4 control-label no_padding_right">应用主体:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size" name="mainbody_id" id="mbList">
					          <option value="">请选择</option>
					        </select>
					      </div>
					    </div>

			        	<div class="form-group domainGroup">
					      <label class="col-xs-4 control-label no_padding_right">应用领域:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size app_domain">
					        	<option value="">请选择</option>
					        </select>
					      </div>
					      <!-- <div class="col-xs-3 hide">
					        <select class="form-control input_size child_domain">
					        	<option value="">请选择</option>
					        </select>
					      </div> -->
					      <div class="col-xs-3 no_padding_both">
					      	<button class="btn btn-primary" id="addDomain">添加</button>
					      </div>
					    </div>



					    <div class="form-group saveTechGroup">
					      <label class="col-xs-4 control-label no_padding_right">节能技术:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size save_tech">
					        	<option value="">请选择</option>
					        </select>
					      </div>
					      <div class="col-xs-4 no_padding_both">
					      	<button class="btn btn-primary" id="addTech">添加</button>
					      </div>
					    </div>

					    <div class="form-group saveDevice">
					      <label class="col-xs-4 control-label no_padding_right">使用的设备:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size save_device">
					        	<option value="">请选择</option>
					        </select>
					      </div>
					      <div class="col-xs-4 no_padding_both">
					      	<button class="btn btn-primary" id="addDevice">添加</button>
					      </div>
					    </div>

					    <div class="form-group saveUseTech">
					      <label class="col-xs-4 control-label no_padding_right">使用的技术:</label>
					      <div class="col-xs-4">
					        <select class="form-control input_size save_use_tech">
					        	<option value="">请选择</option>
					        </select>
					      </div>
					      <div class="col-xs-4 no_padding_both">
					      	<button class="btn btn-primary" id="addTechnology">添加</button>
					      </div>
					    </div>

					    <div class="form-group">
				          <label for="saveRequire" class="col-xs-4 control-label no_padding_right">节能需求:</label>
				          <div class="col-xs-4">
				            <textarea class="form-control text_size" name="save_require" rows="3" id="saveRequire"></textarea>
				            <span class="help-block"></span>
				          </div>
				        </div>

				    	<div class="form-group">
				          <label for="textArea" class="col-xs-4 control-label no_padding_right">项目简介:</label>
				          <div class="col-xs-4">
				            <textarea class="form-control text_size" name="description" rows="3" id="textArea"></textarea>
				            <span class="help-block"></span>
				          </div>
				        </div>

					    <div class="form-group" >
					      <label class="col-xs-4 control-label no_padding_right">项目主要内容:</label>
					      <div class="col-xs-7">
					      	<div id="container" style="height:500px;"></div>
					      </div>
					    </div>

					    <div class="form-group">
					      <div class="col-xs-12 text-center">
					        <button type="button" id="sub" class="btn btn-primary"> 提 交 </button>
					      </div>
					    </div>
				    </div>
				</div>
			</div>
		</div>
	</div>
</div>

@include("admin.footer")
@include("admin.common")
</body>

<script type="text/html" id="mainbodyTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>


<script type="text/html" id="domainTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.cate_name}</option>
{%/each}
</script>

<script type="text/html" id="deviceTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>

<script type="text/html" id="techTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.tech_name}</option>
{%/each}
</script>

<script type="text/html" id="areaTemp">
	<option value="">请选择</option>
	{%each list as value}
	<option value="{%value.area_id}">{%value.area_name}</option>
	{%/each}
</script>

<script type="text/html" id="domainSelectTemp">
	<div class="form-group domainGroup">
	  <label class="col-xs-4 control-label no_padding_right"></label>
	  <div class="col-xs-2">
	    <select class="form-control input_size app_domain"></select>
	  </div>
	  <div class="col-xs-2 hide">
	    <select class="form-control input_size child_domain"></select>
	  </div>
	  <div class="col-xs-2 no_padding_both">
	  	<button class="btn btn-warning deleteTech">删除</button>
	  </div>
	</div>
</script>

<script type="text/html" id="saveTechTemp">
	<div class="form-group saveTechGroup">
      <label class="col-xs-4 control-label no_padding_right"></label>
      <div class="col-xs-4">
        <select class="form-control input_size save_tech"></select>
      </div>
      <div class="col-xs-4 no_padding_both">
      	<button class="btn btn-warning deleteTech">删除</button>
      </div>
    </div>
</script>

<script type="text/html" id="useDevice">
<div class="form-group saveDevice">
  <label class="col-xs-4 control-label no_padding_right"></label>
  <div class="col-xs-4">
    <select class="form-control input_size save_device"></select>
  </div>
  <div class="col-xs-4 no_padding_both">
  	<button class="btn btn-warning deleteUseDevice">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="useTech">
<div class="form-group saveUseTech">
  <label class="col-xs-4 control-label no_padding_right"></label>
  <div class="col-xs-4">
    <select class="form-control input_size save_use_tech"></select>
  </div>
  <div class="col-xs-4 no_padding_both">
  	<button class="btn btn-warning deleteUseTech">删除</button>
  </div>
</div>
</script>

<script type="text/html" id="modelTemp">
<option value="">请选择</option>
{%each list as value}
<option value="{%value.id}">{%value.name}</option>
{%/each}
</script>



<script type="text/javascript">

waitingDialog.show('正在加载中...', {dialogSize: 'sm', progressType: 'primary'});

var url = "<%url('admin')%>/";
var ue = UE.getEditor('container');
var que = "que";
var provinceId = <%$data['province']%>;

var dids = [];
var curDidIndex = 0;

$(".domainGroup").each(function(index, item){
	var node = $(item).children(".col-xs-2")[0];
	dids.push($(node).attr("did"));
});

renderChildDomain();

function renderChildDomain()
{
	if(curDidIndex >= dids.length){
		startAll();
		return;
	}
	$.getq(que, url + "get_domains?parent_id=" + dids[curDidIndex], function(data){
		if(data.success){
			var html = template("domainTemp", {list: data.data});
			var node = $("div[did='" + dids[curDidIndex] + "']");
			var selectNode = node.next().children("select");
			selectNode.html(html);
			curDidIndex++;
			renderChildDomain();
		}
	});
}

function startAll(){
	$.getq(que, url+"get_mainbody",function(data){
		if (data.success){
			var html = template("mainbodyTemp",{list : data.data});
			$("select[name='mainbody_id']").html(html);
		};
	})
	$.getq(que, url+"address/getarea",function (data){
		if(data.success){
			var html = template("areaTemp",{list: data.area})
			$("#province").html(html);
			$("#province").val(provinceId);
		}
	})
	$("#province").bind("change",function(){
		var pid = $(this).val();
		$.getq(que, url+"address/getarea",{ pid : pid},function (data){
			var html = template("areaTemp",{list: data.area})
			$("#city").html(html);
		})
	})
	$("#city").bind("change",function(){
		var pid = $(this).val();
		$.getq(que, url+"address/getarea",{ pid : pid},function (data){
			var html = template("areaTemp",{list: data.area})
			$("#area").html(html);
		})
	})

	$.getq(que, url+"get_investment_type", function (data){
		if(data.success){
			var html = template("modelTemp",{list : data.data})
			$("select[name='investment_type_id']").html(html);
		}
	})


	$('#projectImg').AjaxFileUpload({
		path : url+"upload",
		onComplete : function(filename){
				$(this).siblings('input[class="hiddenName"]').val(filename.filename);
				$(this).parent(".file").siblings(".fileName").html(filename.filename);
			}
		});


	var appDomainSelect;
	var dom;
	$.getq(que, url+"get_domains", function(data){
		if(data.success){
			dom = data.data;
			var html = template("domainTemp", {list: dom});
			$(".app_domain").last().html(html);
			$(".app_domain").last().bind("change", function(){
				appDomainSelect = $(this);
				console.log(appDomainSelect)
				$.getq(que, url+"get_domains?parent_id=" + $(this).val(), function(data){
					if(data.success){
						var html = template("domainTemp", {list: data.data});
						appDomainSelect.parent().next().removeClass("hide");
						appDomainSelect.parent().next().find("select").html(html);
					}
				});
			});
		}
	});


	$("#addDomain").bind("click", function(){
		var html = template("domainSelectTemp", {});
		$(html).insertAfter($(".domainGroup").last());
		$(".deleteTech").unbind("click");
		$(".deleteTech").bind("click", function(){
			$(this).parents(".domainGroup").remove();
		})
		var html = template("domainTemp", {list: dom});
		$(".app_domain").last().html(html);
		$(".app_domain").last().bind("change", function(){
			appDomainSelect = $(this);
			$.getq(que, url+"get_domains?parent_id=" + $(this).val(), function(data){
				if(data.success && data.data.length){
					var html = template("domainTemp", {list: data.data});
					appDomainSelect.parent().next().removeClass("hide");
					appDomainSelect.parent().next().find("select").html(html);
				}
			});
		});
	});

	// 添加使用的设备
	var dev;
	$.getq(que, url+"get_use_device", function (data){
		if(data.success && data.data.length){
			dev = data.data;
			var html = template('deviceTemp', {list :dev});
			$(".save_device").last().html(html);
		}
	})

	$("#addDevice").bind("click", function(){
		var html = template("useDevice", {});
		$(html).insertAfter($(".saveDevice").last());
		var html = template('deviceTemp', {list :dev});
		$(".save_device").last().html(html);
		$(".deleteUseDevice").unbind("click");
		$(".deleteUseDevice").bind("click", function(){
			$(this).parents(".saveDevice").remove();
		})
	})

	//添加使用技术
	var useTec;
	$.getq(que, url+"get_use_tech", function (data){
		if(data.success && data.data.length){
			useTec = data.data
			var html = template("techTemp", {list : useTec});
			$(".save_use_tech").last().html(html);
		}
	})

	$("#addTechnology").bind("click", function(){
		var html = template("useTech", {});
		$(html).insertAfter($(".saveUseTech").last());
		var html = template("techTemp", {list : useTec});
		$(".save_use_tech").last().html(html);
		$(".deleteUseTech").unbind("click");
		$(".deleteUseTech").bind("click", function(){
			$(this).parents(".saveUseTech").remove();
		})
	})

	// 应用技术

	var tec;
	$.getq(que, url+"get_techs", function(data){
		if(data.success && data.data.length){
			tec = data.data;
			var html = template("domainTemp", {list: tec});
			$(".save_tech").last().html(html);
		}
	});


	$("#addTech").bind("click", function(){
		var html = template("saveTechTemp", {});
		$(html).insertAfter($(".saveTechGroup").last());
		var html = template("domainTemp", {list: tec});
		$(".save_tech").last().html(html);
		$(".deleteTech").unbind("click");
		$(".deleteTech").bind("click", function(){
			$(this).parents(".saveTechGroup").remove();
		})
	});
	var province_id;
	var city_id;
	var area_id
	if("<%Input::has('pid')%>"){
		var pid = "<%Input::get('pid')%>";
		$.getq(que, url+"project/get_detail", {pid :pid},function(data){
			if(data.success){
				$("input[name='name']").val(data.data.name);
				$("input[name='investment_scale']").val(data.data.investment_scale);
				$("input[name='efficiency']").val(data.data.efficiency);
				$("input[name='start_time']").val(data.data.start_time);
				$("input[name='end_time']").val(data.data.end_time);
				$("input[name='address']").val(data.data.address);
				$("input[name='image']").val(data.data.image);
				$("textarea[name='save_require']").val(data.data.save_require);
				$("textarea[name='description']").val(data.data.description);
				ue.ready(function(){
				    ue.setContent(decodeURIComponent(data.data.content));
				});
				$("select[name='investment_type_id']").val(data.data.investment_type_id);
				$("select[name='mainbody_id']").val(data.data.main_body[0].mb_id);

				$.each(data.data.project_tech, function(k, v){
					$(".save_tech").eq(k).val(v.cate_id);
					$("#addTech").click();
				})
				$(".deleteTech").last().click();


				$.each(data.data.use_device, function(k, v){
					$(".save_device").eq(k).val(v.device_id);
					$("#addDevice").click();
				})
				$(".deleteUseDevice").last().click();

				$.each(data.data.use_tech, function(k, v){
					$(".save_use_tech").eq(k).val(v.tech_id);
					$("#addTechnology").click();
				})
				$(".deleteUseTech").last().click();

				$.each(data.data.project_domain, function(k, v){
					var node = $(".app_domain").eq(k);
					node.val(node.parent().attr("did"));
					var childNode = node.parent().next().children("select");
					childNode.val(node.parent().attr("cid"));
				})
				$("select[name='province']").val(data.data.province.area_id);
				province_id = data.data.province.area_id;
				city_id = data.data.city.area_id;
				area_id = data.data.area.area_id;
				$("input[name='is_request_assets'][value="+data.data.is_request_assets+"]").attr("checked", "checked");

				$("input[name='is_request_award'][value="+data.data.is_request_award+"]").attr("checked", "checked");

				$("input[name='is_request_subsidy'][value="+data.data.is_request_subsidy+"]").attr("checked", "checked");

				$("input[name='is_request_rebate'][value="+data.data.is_request_rebate+"]").attr("checked", "checked");

				$.getq(que, url+"address/getarea", {pid : province_id}, function (data){
					if(data.success){
						var html = template("areaTemp",{list: data.area})
						$("#city").html(html);
						$("#city").val(city_id);
					}
				})
				$.getq(que, url+"address/getarea", {pid : city_id}, function (data){
					waitingDialog.hide();
					if(data.success){
						var html = template("areaTemp",{list: data.area})
						$("#area").html(html);
						$("#area").val(area_id);
					}
				})
			}
		})
	}









	$("#sub").click(function(){
// is_request_assets
		var name = $("input[name='name']").val().trim();
		var investment_scale = $("input[name='investment_scale']").val().trim();
		var investment_type_id = $("select[name='investment_type_id']").val().trim();
		var efficiency = $("input[name='efficiency']").val().trim();
		var start_time = $("input[name='start_time']").val().trim();
		var end_time = $("input[name='end_time']").val().trim();
		var province = $("select[name='province']").val().trim();
		var city = $("select[name='city']").val().trim();
		var area = $("select[name='area']").val().trim();
		var address = $("input[name='address']").val().trim();
		var is_request_assets = $("input[name='is_request_assets']:checked").val().trim();
		var is_request_award = $("input[name='is_request_award']:checked").val().trim();
		var is_request_subsidy = $("input[name='is_request_subsidy']:checked").val().trim();
		var is_request_rebate = $("input[name='is_request_rebate']:checked").val().trim();
		var image = $("input[name='image']").val().trim();
		var description = $("textarea[name='description']").val().trim();
		var save_require = $("textarea[name='save_require']").val().trim();
		var content = encodeURIComponent(ue.getContent());
		var mainBody = $("#mbList").val();


		if (name == '' || investment_scale == '' || investment_type_id == '' || efficiency == '' || start_time == '' || end_time == '' || province < 1  || city < 1  || address == ''|| is_request_assets == ''|| is_request_award == '' || is_request_subsidy == '' || is_request_rebate == '' || description == '' || content == '' || mainBody == '' || save_require == '')
		{
			warning('','必填信息不能为空');
			return false;
		}

		var domains = [];
		$.each($(".app_domain"), function(index, item){
			if($(item).parent().next().hasClass("hide")){
				domains.push($(item).val());
			}else{
				domains.push($(item).parent().next().find("select").val());
			}
		});

		var bool = false;
		$.each(domains, function(index, item){
			if(item == ""){
				warning("", "请选择应用领域！");
				bool = true;
				return true;
			}
		});

		if(bool){
			return;
		}

		var techs = [];
		$.each($(".save_tech"), function(index, item){
			if($(item).val()){
				techs.push($(item).val());
			}
		});
		if(techs.length == 0){
			warning("", "请选节能技术");
			return;
		}

		var device = [];
		$.each($(".save_device"), function (index, item){
			if($(item).val()){
				device.push($(item).val());
			}
		})
		if(device.length == 0){
			warning("", "请选择使用的设备");
			return;
		}

		var useTech = [];
		$.each($(".save_use_tech"), function (index, item){
			if($(item).val()){
				useTech.push($(item).val());
			}
		})
		if(useTech.length == 0){
			warning("", "请选择使用的技术");
			return;
		}
		$.post(url+"project/edit_project/update", {
			name : name,
			investment_scale : investment_scale,
			investment_type_id : investment_type_id,
			efficiency : efficiency,
			end_time : end_time,
			province : province,
			city : city,
			area : area,
			address : address,
			is_request_assets :is_request_assets,
			is_request_award : is_request_award,
			is_request_subsidy : is_request_subsidy,
			is_request_rebate : is_request_rebate,
			image : image,
			description : description,
			content : content,
			domains: domains,
			techs: techs,
			main_body: mainBody,
			save_require: save_require,
			start_time: start_time,
			device : device,
			useTech : useTech,
			pid : pid,
			uid:uid
		},function (data){
			if(data.success){
				success("", "提交成功");
				setTimeout(function(){
					window.location.href=url+"project";
				},2000)
			}
		})
	})
}

</script>

</html>